package com.sy.service.impl;

import com.sy.bean.GoodsWarehouse;
import com.sy.bean.SaveGoods;
import com.sy.common.crud.BaseServiceImpl;
import com.sy.common.exception.BusinessException;
import com.sy.mapper.SaveGoodsMapper;
import com.sy.mapper.WarehouseGoodsMapper;
import com.sy.service.SaveGoodsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

@Service
public class SaveGoodsServiceImpl extends BaseServiceImpl<SaveGoods, SaveGoodsMapper> implements SaveGoodsService {
    @Autowired
    WarehouseGoodsMapper warehouseGoodsMapper;

    @Autowired
    SaveGoodsMapper saveGoodsMapper;

    @Override
    public int save(SaveGoods entity) {
        int rs = super.save(entity);
        if (rs==0)
            throw new BusinessException("新增商品失败");
        rs = warehouseGoodsMapper.batchSave(entity.getId(),entity.getWarehouseGoodsList());
        if (rs<1)
            throw new BusinessException("新增仓库库存信息失败");
        return 1;
    }

    public int update(SaveGoods saveGoods){
        //修改商品
        int rs = baseMapper.update(saveGoods);
        if (rs == 0){
            throw new BusinessException("修改商品失败");
        }
        //先删除之前的商品明细
        saveGoodsMapper.deleteByGoodsId(saveGoods.getId());
        //生成新的明细
        saveGoodsMapper.saveGoodsBatch(saveGoods.getId(),saveGoods.getWarehouseGoodsList());
        if (rs == 0){
            throw new BusinessException("更新商品明细失败");
        }
        return rs;
    }

    @Override
    public List<GoodsWarehouse> queryGoodsWarehouse(Map map) {
        return saveGoodsMapper.queryGoodsWarehouse(map);
    }
}
